package com.edu.exam.enums.grading;

import com.edu.exam.enums.core.BaseEnum;

/**
 * 规则创建者类型枚举
 *
 * <p>定义验证规则创建者的类型，包括系统、管理员、教师等。</p>
 *
 * @author System
 * @version 1.0.0
 * @since 2025-11-14
 */
public enum RuleCreatorType implements BaseEnum {

    /** 系统 */
    SYSTEM("SYSTEM", "系统", "系统预置的默认规则"),

    /** 管理员 */
    ADMIN("ADMIN", "管理员", "系统管理员创建的规则"),

    /** 教师 */
    TEACHER("TEACHER", "教师", "教师自定义的规则");

    private final String code;
    private final String description;
    private final String detail;

    RuleCreatorType(String code, String description, String detail) {
        this.code = code;
        this.description = description;
        this.detail = detail;
    }

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public String getDescription() {
        return description;
    }

    public String getDetail() {
        return detail;
    }

    /**
     * 从代码获取枚举值
     *
     * @param code 代码
     * @return 对应的枚举值
     * @throws IllegalArgumentException 如果代码无效
     */
    public static RuleCreatorType fromCode(String code) {
        for (RuleCreatorType type : values()) {
            if (type.getCode().equals(code)) {
                return type;
            }
        }
        throw new IllegalArgumentException("Unknown rule creator type code: " + code);
    }

    /**
     * 从描述获取枚举值
     *
     * @param description 描述
     * @return 对应的枚举值
     * @throws IllegalArgumentException 如果描述无效
     */
    public static RuleCreatorType fromDescription(String description) {
        for (RuleCreatorType type : values()) {
            if (type.getDescription().equals(description)) {
                return type;
            }
        }
        throw new IllegalArgumentException("Unknown rule creator type description: " + description);
    }

    /**
     * 检查是否为系统创建
     */
    public boolean isSystemCreated() {
        return this == SYSTEM;
    }

    /**
     * 检查是否为管理员创建
     */
    public boolean isAdminCreated() {
        return this == ADMIN;
    }

    /**
     * 检查是否为教师创建
     */
    public boolean isTeacherCreated() {
        return this == TEACHER;
    }

    /**
     * 检查是否为用户创建（非系统）
     */
    public boolean isUserCreated() {
        return this == ADMIN || this == TEACHER;
    }

    /**
     * 获取创建权限级别
     */
    public int getPermissionLevel() {
        switch (this) {
            case SYSTEM:
                return 0;  // 系统级别最高
            case ADMIN:
                return 1;  // 管理员级别
            case TEACHER:
                return 2;  // 教师级别
            default:
                return 9;  // 其他级别
        }
    }

    @Override
    public String toString() {
        return description + " - " + detail;
    }
}